﻿using System;
using System.IO;
using System.Reflection;

namespace GenericCommTool
{
    class Program
    {
        static void Main(string[] args)
        {
            var input = string.Empty;
            do
            {
                var projectName = GetInput("input the project name(namespace of the basic):");
                var projectPath = GetInput("input the project basic path for generic repository/service(default for this project):", true);
                var className = GetInput("input the class name:");
                var classDesc = GetInput("input the class description:");
                projectPath = projectPath.IsNullOrEmpty() ?
                    DefaultPath(projectName)
                    : projectPath;
                IGenericService genericService = new GenericService(projectPath, projectName);
                var result = genericService.Generic(className, classDesc);
                Output($"generic the result for {className}:\r\n{result}", true);
                Output("do you need to continue?(Y/n):");
            } while (input.IsQuit());
        }

        static string DefaultPath(string projectName)
        {
            var location = Assembly.GetExecutingAssembly().Location;
            var index = location.IndexOf("GenericCommTool");
            var res = location.Substring(0, index).Replace("\\tools", "");
            return res;
        }

        static string GetInput(string msg, bool bEmpty = false)
        {
            string str = string.Empty;
            do
            {
                Output(msg, true);
                str = Console.ReadLine();
            } while (!bEmpty && str.IsNullOrEmpty());

            return str;
        }

        static void Output(string msg, bool bRet = true)
        {
            if (bRet)
                Console.WriteLine(msg);
            else
                Console.Write(msg);
        }
    }
}
